//
//  ActivateUserViewController.m
//  e-nurse
//
//  Created by  on 1/2/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "ActivateUserViewController.h"
#import "ServerProxy.h"
#import "ActivateResult.h"
#import "ResendActivateCodeResult.h"
#import "ALToastView.h"

@interface ActivateUserViewController ()

- (void)resendActivateCodeCompleted:(ResendActivateCodeResult*)activateResult;
- (void)resendActivateCodeCompletedImpl:(ResendActivateCodeResult*)activateResult;

- (void)activateCompleted:(ActivateResult*)activateResult;
- (void)activateCompletedImpl:(ActivateResult *)activateResult;

-(BOOL)checkInput;

@end

@implementation ActivateUserViewController
@synthesize userNameTextField = _userNameTextField;
@synthesize activateCodeTextField = _activateCodeTextField;
@synthesize userId = _userId;
@synthesize userName = _userName;
@synthesize apiKey = _apiKey;

- (void)dealloc
{
    self.apiKey = nil;
    self.userName = nil;
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.userNameTextField.text = self.userName;
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    self.userNameTextField = nil;
    self.activateCodeTextField = nil;
    self.userName = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)closeButtonClicked:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (IBAction)activateButtonClicked:(id)sender
{
    [self.view endEditing:YES];
    if([self checkInput])
    {
        [[ServerProxy sharedInstance] activate:self.userId activateCode:[self.activateCodeTextField.text intValue] userName:self.userName apiKey:self.apiKey completedAction:@selector(activateCompleted:) target:self];
    }
}

- (void)activateCompleted:(ActivateResult*)activateResult
{
    [activateResult retain];
    [self performSelectorOnMainThread:@selector(activateCompletedImpl:) withObject:nil waitUntilDone:NO];
}

- (void)activateCompletedImpl:(ActivateResult *)activateResult
{
    switch (activateResult.errorCode) {
        case 0:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_ACTICATE_SUCCESSFUL", nil)];
            break;
        case 1:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_ACTIVATE_CODE_FORMAT_INVALID", nil)];
            break;  
        case 2:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_ACTIVATE_CODE_INVALID", nil)];
            break;   
        case 3:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_ACTIVATE_CODE_EXPIRED", nil)];
            break; 
        default:
            break;
    }
    [activateResult release];
}

- (IBAction)resendActivateCodeButtonClicked:(id)sender
{
    [self.view endEditing:YES];

    [[ServerProxy sharedInstance] resentActivateCode:self.userId userName:self.userName apiKey:self.apiKey completedAction:@selector(resendActivateCodeCompleted:) target:self];
}

- (void)resendActivateCodeCompleted:(ResendActivateCodeResult *)activateResult
{
    [activateResult retain];
    [self performSelectorOnMainThread:@selector(resendActivateCodeCompletedImpl:) withObject:activateResult waitUntilDone:NO];
}


- (void)resendActivateCodeCompletedImpl:(ResendActivateCodeResult *)activateResult
{
    switch (activateResult.errorCode) {
        case 0:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_ACTICATE_CODE_SENT", nil)];
            break;
        case 1:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_ACTIVATE_CODE_SENT_REPATELY", nil)];
            break; 
        case 2:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_USER_NOT_EXISTING", nil)];
            break; 
        case 3:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_USER_IS_ACTIVATED", nil)];
            break; 
        default:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_CONNECT_SERVER_FAILED", nil)];
            break;
    }
    [activateResult release];
}

-(BOOL)checkInput
{
    if([self.activateCodeTextField.text isEqualToString:@""])
    {
        [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_ACTIVATE_CODE_CANT_EMPTY", nil)];
        return false;
    }   
    else
    {
        return  true;
    }
}

@end
